package com.sunnuwell.demo.controller;

import com.alibaba.fastjson.JSON;
import com.sunnuwell.demo.annotation.UserLoginToken;
import com.sunnuwell.demo.common.exception.AdException;
import com.sunnuwell.demo.entity.Resource;
import com.sunnuwell.demo.service.ResourceService;
import com.sunnuwell.demo.vo.ResourceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: entry
 * @description: 资源菜单controller
 * @author: Mr.lv
 * @create: 2019-04-28 09:33
 **/
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class ResourceController {

    private ResourceService resourceService;

    public ResourceController(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    /**
     * @Description: 获取所有的资源菜单
     * @Param: []
     * @return: com.sunnuwell.demo.entity.Resource
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @GetMapping("/getAllResource")
    @UserLoginToken
    public List<Resource> getAllResource() {
        try {
            // 获取所有的资源菜单
            List<Resource> resourceList = resourceService.getAllResource();
            return resourceList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Description: 新增/修改菜单资源
     * @Param: [resource]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @PostMapping("/addResource")
    @UserLoginToken
    public String addResource(@RequestBody ResourceVo resource) throws AdException {
        log.info("ResourceController: add -> {}",
                JSON.toJSONString(resource));
        return resourceService.addResource(resource);
    }

    /**
     * @Description: 删除资源
     * @Param: [resourceVo]
     * @return: com.sunnuwell.demo.common.vo.CommonResponse
     * @Author: Mr.Lv
     * @Date: 2019/4/28
     */
    @DeleteMapping("/deleteResource")
    @UserLoginToken
    public String deleteResource(@RequestBody ResourceVo resourceVo) throws AdException {
        log.info("ResourceController: delete_id -> {}",
                resourceVo.getRid());
        return resourceService.deleteResource(resourceVo);
    }
}